<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="赛事名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入赛事名称"
        ></el-input>
      </el-form-item>
      <!-- <el-form-item label="开始时间">
        <el-date-picker
          v-model="queryParams.start_time"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fetchData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" type="index" width="100" />
      <el-table-column prop="name" label="赛事名称" width="300">
      </el-table-column>
      <el-table-column prop="province" label="省份" width="150">
      </el-table-column>
      <el-table-column prop="city" label="城市" width="200"> </el-table-column>
      <el-table-column prop="district" label="区县" width="200">
      </el-table-column>
      <el-table-column prop="start_time" label="开始时间" width="200">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            icon="el-icon-edit"
            size="mini"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleConfig(scope.row)"
            icon="el-icon-setting"
            size="mini"
            >配置</el-button
          >
          <el-button
            type="text"
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="fetchData"
    />

    <!-- 添加或修改比赛对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      @close="reset"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="赛事名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入赛事名称" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <!-- 放置上传图片 -->
          <image-upload :limit="1" v-model="form.imageUrl"></image-upload>
        </el-form-item>
        <el-form-item label="省市区" prop="area">
          <el-cascader
            size="large"
            :options="options"
            v-model="selectedOptions"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="开始时间" prop="start_time">
          <el-date-picker
            v-model="form.start_time"
            type="datetime"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  regionData,
  codeToText,
  textToCode,
  pcaTextArr,
} from "element-china-area-data";
import { addRace, delRace, getInfo, listRace, editRace } from "../../../api/race/manager";
export default {
  name: "RaceList",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {
        // 城市
        city: "",
        // 创建人
        creater: "11",
        // 区
        district: "",
        // id
        id: "",
        // 图片url
        imageUrl: "",
        // 名称
        name: "",
        // 省
        province: "",
        // 开始时间
        start_time: "",
        // 备用
        title: "11",
      },
      // 部门树选项
      deptOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        // start_time: [],
      },
      fetchData: [],
      options: pcaTextArr,
      selectedOptions: [],
      total: 50,
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入比赛名称", trigger: "blur" }],
        // area: [{ required: true, message: "请选择地区", trigger: "blur" }],
        imageUrl: [{ required: true, message: "请上传图片", trigger: "blur" }],
        start_time: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询赛事列表 */
    getList() {
      this.loading = true;

      listRace(this.queryParams).then((response) => {
        this.fetchData = response.rows;
        this.loading = false;
      });
    },
    // 新增
    handleAdd() {
      this.open = true;
      this.title = "新建赛事";
    },
    // 修改
    handleUpdate(row) {
      const raceId = row.id || this.ids;
      console.log(row);
      getInfo(raceId).then((response) => {
        var tempData = [];
        this.form = response.data;
        const province = response.data.province;
        const city = response.data.city;
        const district = response.data.district;
        if (
          response.data.province != null &&
          response.data.city != null &&
          response.data.district != null
        ) {
          tempData.push(province);
          tempData.push(city);
          tempData.push(district);
          this.selectedOptions = tempData;
        }
        this.open = true;
        this.title = "修改赛事";
      });
    },
    // 比赛配置
    handleConfig(row) {
      const raceId = row.id;
      this.$router.push("/race/config/index/" + raceId);
    },
    // 删除
    handleDelete(row) {
      const userIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除用户编号为"' + userIds + '"的数据项？')
        .then(function () {
          return delRace(userIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    // 提交
    submitForm() {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            editRace(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.selectedOptions = [];
              this.open = false;
              this.getList();
            });
          } else {
            addRace(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              this.selectedOptions = [];
              this.open = false;
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 查询
    handleQuery() {
      console.log(this.queryParams);
      this.getList();
    },
    // 重置查询条件
    resetQuery() {
      this.queryParams = {};
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map((item) => item.id);
      console.log(this.ids);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 地址级联发生改变
    handleChange(value) {
      console.log(this.selectedOptions);
      if (value[0] != null && value[1] != null && value[2] != null) {
        this.form.province = value[0];
        this.form.city = value[1];
        this.form.district = value[2];
      }
    },
    // 导出
    handleExport() {},
  },
};
</script>
